<?php

namespace App\Models;

use Exception;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

/*总数字资源类型model*/

class ResourceHotSearch extends BaseModel
{
    use HasFactory;

    const CREATED_AT = 'create_time';
    const UPDATED_AT = null;


    protected $table = 'resource_hot_search';


    /**
     * 添加检索次数
     * @param $content
     * @param $type
     */
    public function hotSearchAdd($content, $type)
    {
        $res = $this->where('content', $content)
            ->where(function ($query) use ($type) {
                if ($type) {
                    $query->where('type', $type);
                }
            })->first();
        if (!empty($res)) {
            $res->increment('number');
        } else {
            $this->add([
                'content' => $content,
                'number' => 1,
                'type' => $type,
            ]);
        }
    }


    /**
     *获取检索列表
     * @param type 暂时无用
     */
    public function lists($type, $limit = null)
    {
        $res = $this->select('content')
            ->where(function ($query) use ($type) {
                if ($type) {
                    $query->where('type', $type);
                }
            })
            ->orderByDesc('number')
            ->paginate($limit)
            ->toArray();
        return $res;
    }
}
